package com.example.liaotian.Fragment.DownFragment;


import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.liaotian.Fragment.UpFragment.ActFragment;
import com.example.liaotian.Fragment.UpFragment.HomeUpFragment;
import com.example.liaotian.Fragment.UpFragment.ServiceFragment;
import com.example.liaotian.Fragment.UpFragment.TalkFragment;
import com.example.liaotian.Fragment.UpFragment.TransactionFragment;
import com.example.liaotian.R;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * 分页集市页
 * A simple {@link Fragment} subclass.
 */
public class MarketFragment extends Fragment {


    private List<Fragment> fragments;
    private ArrayList<String> title;
    private TabLayout tabLayout;
    private ViewPager viewPager;

    public MarketFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_market, container, false);
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initView();

    }

    /**
     * 绑定组件
     * 填入数据
     * 加载组件
     */
    private void initView() {

        fragments = new ArrayList<>();
        title = new ArrayList<>();

        title.add("社区服务");
        title.add("跳蚤市场");
        fragments.add(new ServiceFragment());
        fragments.add(new TransactionFragment());


        tabLayout = getView().findViewById(R.id.tabLayout2);
        viewPager = getView().findViewById(R.id.viewPager2);

        /**
         * 这里用了匿名对象的方式来创建适配器
         *
         */
        viewPager.setAdapter(new FragmentStatePagerAdapter(getFragmentManager()) {
            /**
             * 把每个fragment填入每个pager
             * @param position
             * @return
             */
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            /**
             * pager的长度就是fragment的数量
             * @return
             */
            @Override
            public int getCount() {
                return fragments.size();
            }

            /**
             * 给tablayout获取标题
             * @param position
             * @return
             */
            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return title.get(position);
            }
        });
        /**
         *把pager和tablayout绑定
         */
        tabLayout.setupWithViewPager(viewPager);
    }


}
